package top.xcxdl.rural.utils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Ding
 * @create 2022/4/10 11:08
 */
@Component
public class RedisUtils {

    private static RedisTemplate redisTemplate;

    @Resource
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisUtils.redisTemplate = redisTemplate;
    }

    /**
     * 存储验证码
     * @param key 手机号
     * @param value 验证码
     * @param time 过期时间
     */
    public static void setCheckCode(String key, Object value, long time) {
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }

    /**
     * 获取验证码
     * @param key 手机号
     * @return
     */
    public static Object getCheckCode(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 修改客服电话
     */
    public static void changeService(List<String> serviceTels) {
        //先全部内存中的数据
        redisTemplate.delete("serviceTel");
        //再检查List中数据是否存在空值
        for (int i = 0; i < serviceTels.size(); i++) {
            if (serviceTels.get(i) == null || "".equals(serviceTels.get(i)) || "null".equals(serviceTels.get(i))) {
                serviceTels.remove(i--);
            }
        }
        //再添加
        redisTemplate.opsForList().rightPushAll("serviceTel",serviceTels);
    }

    /**
     * 获取客服电话
     */
    public static List<String> getService() {
        List<String> serviceTels = redisTemplate.opsForList().range("serviceTel", 0, -1);
        return serviceTels;
    }


}
